<template>
	<uni-popup ref="popup" type="bottom">
		<view class="sharePopup">分享到</view>
		<view class="bottomPopup">
			<view v-for="(item,index) in providerList" :key="index" hover-start-time=15 hover-stay-time=200 hover-class="bgColor"
			 @tap="share(item)">
				<text class="iconfont" :class="item.font"></text>
				<text>{{item.name}}</text>
			</view>
		</view>
		<view class="cancel" hover-start-time=15 hover-stay-time=200 hover-class="bgColor" @tap="cancel">取消</view>
	</uni-popup>
</template>

<script>
	import uniPopup from "@/components/uni-popup/uni-popup.vue";
	export default {
		components: {
			uniPopup
		},
		mounted() {
			uni.getProvider({
				service: 'share',
				success: (e) => {
					console.log('success', e);
					let data = []
					for (let i = 0; i < e.provider.length; i++) {
						switch (e.provider[i]) {
							case 'weixin':
								data.push({
									name: '微信好友',
									id: 'weixin',
									sort: 0,
									font: "icon-weixin"
								})
								data.push({
									name: '朋友圈',
									id: 'weixin',
									type: 'WXSenceTimeline',
									sort: 1,
									font: "icon-pengyouquan"
								})
								break;
							case 'sinaweibo':
								data.push({
									name: '新浪微博',
									id: 'sinaweibo',
									sort: 2,
									font: "icon-weibo"
								})
								break;
							case 'qq':
								data.push({
									name: 'QQ好友',
									id: 'qq',
									sort: 3,
									font: "icon-icon"
								})
								break;
							default:
								break;
						}
					}
					this.providerList = data.sort((x, y) => {
						return x.sort - y.sort
					});
				},
				fail: (e) => {
					console.log('获取分享通道失败', e);
					uni.showModal({
						content: '获取分享通道失败',
						showCancel: false
					})
				}
			});
		},
		data() {
			return {
				title: 'share',
				shareText: 'uni-app可以同时发布成原生App、小程序、H5，邀请你一起体验！',
				href: "https://uniapp.dcloud.io",
				image: '',
				shareType: 1,
				// 底部按钮的内容了吧
				providerList: [],
			};
		},
		computed: {
			isDisableButton() {
				return function(item) {
					if (this.shareType === 0 && item.id === 'qq') {
						return true;
					}
					if (this.shareType === 5 && item.name !== '分享到微信好友') {
						return true;
					}
					return false;
				}
			}
		},
		onShareAppMessage() {
			return {
				title: this.shareText ? this.shareText : "欢迎体验uni-app",
				path: '/pages/tabBar/component/component',
				imageUrl: this.image ? this.image : 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png'
			}
		},
		beforeDestroy() {
			this.shareText = 'uni-app可以同时发布成原生App、小程序、H5，邀请你一起体验！',
				this.href = 'https://uniapp.dcloud.io',
				this.image = '';
			console.log(`号能给饿了`)
		},
		methods: {
			open() {
				this.$refs.popup.open();
			},
			close() {
				this.$refs.popup.close();
			},
			cancel() {
				this.$refs.popup.close();
			},
			// 点击分享
			async share(e) {
				console.log('分享通道:' + e.id + '； 分享类型:' + this.shareType);
				this.$refs.popup.close();
				if (!this.shareText && (this.shareType === 1 || this.shareType === 0)) {
					uni.showModal({
						content: '分享内容不能为空',
						showCancel: false
					})
					return;
				}

				if (!this.image && (this.shareType === 2 || this.shareType === 0)) {
					uni.showModal({
						content: '分享图片不能为空',
						showCancel: false
					})
					return;
				}

				let shareOPtions = {
					provider: e.id,
					scene: e.type && e.type === 'WXSenceTimeline' ? 'WXSenceTimeline' : 'WXSceneSession', //WXSceneSession”分享到聊天界面，“WXSenceTimeline”分享到朋友圈，“WXSceneFavorite”分享到微信收藏     
					type: this.shareType,
					success: (e) => {
						console.log('success', e);
						uni.showToast({
							title: `分享成功`,
							icon: "success"
						});
					},
					fail: (e) => {
						console.log('fail', e)
						uni.showToast({
							title: `分享失败`,
							icon: "none"
						});
					},
					complete: function() {
						console.log('分享操作结束!')
					}
				}

				switch (this.shareType) {
					case 0:
						shareOPtions.summary = this.shareText;
						shareOPtions.imageUrl = this.image;
						shareOPtions.title = '欢迎体验uniapp';
						shareOPtions.href = 'https://uniapp.dcloud.io';
						break;
					case 1:
						shareOPtions.summary = this.shareText;
						break;
					case 2:
						shareOPtions.imageUrl = this.image;
						break;
					case 5:
						shareOPtions.imageUrl = this.image ? this.image :
							'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png'
						shareOPtions.title = '欢迎体验uniapp';
						shareOPtions.miniProgram = {
							id: 'gh_33446d7f7a26',
							path: '/pages/tabBar/component/component',
							webUrl: 'https://uniapp.dcloud.io',
							type: 0
						};
						break;
					default:
						break;
				}

				if (shareOPtions.type === 0 && plus.os.name === 'iOS') { //如果是图文分享，且是ios平台，则压缩图片 
					shareOPtions.imageUrl = await this.compress();
				}
				if (shareOPtions.type === 1 && shareOPtions.provider === 'qq') { //如果是分享文字到qq，则必须加上href和title
					shareOPtions.href = 'https://uniapp.dcloud.io';
					shareOPtions.title = '欢迎体验uniapp';
				}
				uni.share(shareOPtions);
			},
			compress() { //压缩图片 图文分享要求分享图片大小不能超过20Kb
				console.log('开始压缩');
				let img = this.image;
				return new Promise((res) => {
					var localPath = plus.io.convertAbsoluteFileSystem(img.replace('file://', ''));
					console.log('after' + localPath);
					// 压缩size
					plus.io.resolveLocalFileSystemURL(localPath, (entry) => {
						entry.file((file) => { // 可通过entry对象操作图片 
							console.log('getFile:' + JSON.stringify(file));
							if (file.size > 20480) { // 压缩后size 大于20Kb
								plus.zip.compressImage({
									src: img,
									dst: img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG'),
									width: '10%',
									height: '10%',
									quality: 1,
									overwrite: true
								}, (event) => {
									console.log('success zip****' + event.size);
									let newImg = img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG');
									res(newImg);
								}, function(error) {
									uni.showModal({
										content: '分享图片太大,需要请重新选择图片!',
										showCancel: false
									})
								});
							}
						});
					}, (e) => {
						console.log('Resolve file URL failed: ' + e.message);
						uni.showModal({
							content: '分享图片太大,需要请重新选择图片!',
							showCancel: false
						})
					});
				})
			}
		}
	}
</script>

<style lang="scss">
	// 弹出框的内容
	.sharePopup,
	.cancel,
	.bottomPopup {
		display: flex;
		justify-content: center;
		width: 100vw;
		background-color: #FFFFFF;
		padding: 25rpx 0rpx;
	}

	.sharePopup {
		border-radius: 20rpx 20rpx 0rpx 0rpx;
	}

	.cancel {
		border-top: 1rpx solid #E5E5E5;
	}

	.bottomPopup {
		>view {
			flex: 1;
			@include flexCenterCenter;
			flex-direction: column;

			>text {
				&:first-of-type {
					width: 110rpx;
					height: 110rpx;
					@include flexCenterCenter;
					font-size: 1.9rem;
					border-radius: 50%;
					color: #FFFFFF;
				}

				&:last-of-type {
					margin-top: 10rpx;
					font-size: 0.8rem;
					color: #747474;
				}
			}
		}
	}

	// 底部分享的每个图标的背景色
	.icon-weixin {
		background-color: #0AAD87;
	}

	.icon-pengyouquan {
		background-color: #3D3D3D;
	}

	.icon-weibo {
		background-color: #E24F43;
	}

	.icon-icon {
		background-color: #3669BD;
	}
</style>
